package bch.maven.plugin.guava;

import com.google.common.graph.GraphBuilder;
import com.google.common.graph.MutableGraph;

/*
   精简图示:

       A → B → C
       ↑       │
       └───────┘
             ▼
             D

   说明：
   - 环路：A → B → C → A
   - C 还有一个子节点 D
*/
public class GraphExample {
    public static void main(String[] args) {
        MutableGraph<String> graph = GraphBuilder.directed()
                .allowsSelfLoops(false)
                .build();

        graph.addNode("A");
        graph.addNode("B");
        graph.addNode("C");
        graph.addNode("D");

        graph.putEdge("A", "B"); // A -> B
        graph.putEdge("B", "C"); // B -> C
        graph.putEdge("C", "A"); // C -> A，形成环
        graph.putEdge("C", "D"); // C -> D

        System.out.println(graph.hasEdgeConnecting("A", "B")); // true
        System.out.println(graph.hasEdgeConnecting("A", "C")); // false
        System.out.println(graph.degree("A")); // 2
        System.out.println(graph.inDegree("A")); // 1
        System.out.println(graph.outDegree("A")); // 1
        System.out.println(graph.successors("A")); // [B]
        System.out.println(graph.successors("C")); // [D, A]
        System.out.println(graph.predecessors("A")); // [C]
        System.out.println(graph.predecessors("D")); // [C]
    }
}
